package graphinterface;

import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.geom.Shape;

public class MouseDetector {

	protected static final int NORMAL = 0;
	protected static final int MOUSE_OVER = 1;
	protected static final int MOUSE_DOWN = 2;
	
	protected Shape shape;
	protected String name;
	private boolean contour;
	
	private Color couleursBouton[];
	private Color couleursText[];
	protected int state = NORMAL; 
	protected boolean pushed;

	public MouseDetector(Shape shape, Color c[], String name) throws SlickException{
		this.pushed = false;
		this.shape = shape;
		couleursBouton = c;
		this.contour = true;
		this.name = name;
	}
	
	public MouseDetector(Shape shape, Color c[], Color t[], String name) throws SlickException{
		this.pushed = false;
		this.shape = shape;
		couleursBouton = c;
		couleursText = t;
		this.contour = false;
		this.name = name;
	}
	
	public void draw(Graphics g){
		g.setColor(couleursBouton[state]);
		if(contour){
			g.draw(this.shape);
			g.drawString(this.name, 
					this.shape.getX()+(this.shape.getWidth()-g.getFont().getWidth(this.name))/2,
					this.shape.getY()+(this.shape.getHeight()-g.getFont().getHeight(this.name))/2);
		}else{
			g.fill(shape);
			g.setColor(couleursText[state]);
			g.drawString(this.name, 
					this.shape.getX()+(this.shape.getWidth()-g.getFont().getWidth(this.name))/2,
					this.shape.getY()+(this.shape.getHeight()-g.getFont().getHeight(this.name))/2);
		}
	}
	
	public void update(GameContainer gc, int mouseButton){
		if (this.isInColision(gc.getInput().getMouseX(), gc.getInput().getMouseY())){
			if (gc.getInput().isMouseButtonDown(mouseButton)){
				this.setActual(MOUSE_DOWN);
				pushed = true;
			}else{
				this.setActual(MOUSE_OVER);
			}
		}
		else {
			this.setActual(NORMAL);
		}
	}
	
	public boolean isInColision(int mX, int mY){
		if (this.shape.contains(mX, mY)){
			return true;
		}
		return false;
	}
	
	public void resetPushed(){
		this.pushed = false;
	}
	
	public void setActual(int state){
		this.state = state;
	}
	
	
	public Shape getShape(){
		return this.shape;
	}
	
	public int getActual(){
		return state;
	}
	
	public boolean isPushed(){
		return pushed;
	}
	
	public String getName(){
		return this.name;
	}
}
